#ifndef ALG_DUMB3_HPP
#define ALG_DUMB3_HPP

template<typename T> int signal(T value)
{
    return (value>0?1:(value<0?-1:0));
}

class Dumb3: public PathAlg
{
public:
	Dumb3()
	{
	    //
	}

	void init(Map &map)
	{
        int distanceX, distanceY;
        distanceX = map.dX() - map.sX();
        distanceY = map.dY() - map.sY();

        if(abs(distanceX) > abs(distanceY))
        {
            double proportionY = abs(double(distanceY)/double(distanceX) )*signal(distanceY);
            int proportionX = signal(distanceX);
            int limit = abs(distanceX);
            for(int i=0; i <= limit; ++i)
            {
                map(i*proportionY+ map.sY(), round(i*proportionX)+ map.sX()) |= MARKED;
            }
            _dir1 = (distanceX>0)?EAST:WEST;
            _dir2 = (distanceY>0)?SOUTH:NORTH;
        }
        else if(distanceY == 0)
        {
            map(map.sY(), map.sX()) |= MARKED;
        }
        else // distanceY >= distanceY AND distanceY != 0
        {
            double proportionX = abs(double(distanceX)/double(distanceY) )*signal(distanceX);
            int proportionY = signal(distanceY);
            int limit = abs(distanceY);
            for(int j=0; j <= limit; ++j)
            {
                map(round(j*proportionY) +map.sY(), j*proportionX +map.sX()) |= MARKED;
            }
            _dir1 = (distanceY>0)?SOUTH:NORTH;
            _dir2 = (distanceX>0)?EAST:WEST;
        }
        map(map.dY(), map.dX()) |= MARKED;
	}

	Result step(Map &map)
	{
	    if(map.x() == map.dX() && map.y() == map.dY()) return ALG_SUCCEEDED;
	    if(map.hasFlag(MARKED, _dir1))
	        map.walk(_dir1);
	    else if(map.hasFlag(MARKED, _dir2))
            map.walk(_dir2);
        else if(map.dir() == _dir2)
            map.walk(_dir1);
        else map.walk(_dir2);
	    return ALG_PROCESSING;
	}
private:
    Uint8 _dir1;
    Uint8 _dir2;
};


#endif // ALG_DUMB3_HPP

